package mysql

import (
	"gorm.io/gorm"
	"video-hub/videorpc/videorpc"
)

type Video struct {
	gorm.Model
	UserId        int64  `gorm:"type:int(11);not null;comment:上传用户ID"`
	VideoTitle    string `gorm:"type:varchar(30);not null;comment:视频标题"`
	VideoCategory string `gorm:"type:varchar(30);not null;comment:视频分类"`
	VideoLink     string `gorm:"type:varchar(500);not null;comment:视频URL"`
	CoverLink     string `gorm:"type:varchar(500);comment:视频封面URL"`
	VideoIntro    string `gorm:"type:varchar(500);comment:视频简介"`
	PlayNum       int64  `gorm:"type:int(11);default:0;comment:播放量"`
	LikeNum       int64  `gorm:"type:int(11);default:0;comment:点赞数"`
	CollectNum    int64  `gorm:"type:int(11);default:0;comment:收藏数"`
	CommentNum    int64  `gorm:"type:int(11);default:0;comment:评论数"`
	VideoStatus   int64  `gorm:"type:int(11);default:1;comment:视频状态0-已发布，1-草稿，2-下架"`
	TopicId       int64  `gorm:"type:int(11);comment:关联话题ID"`
}

func (v *Video) VideoAdd(db *gorm.DB) error {
	return db.Debug().Create(v).Error
}

func (v *Video) FirstVideoById(db *gorm.DB, id int64) error {
	return db.Debug().Where("id =?", id).First(v).Error
}

func (v *Video) UpdateVideo(db *gorm.DB) error {
	return db.Debug().Updates(v).Error
}

/*
SELECT

	users.nick_name username,
	videos.id video_id,
	videos.video_title,
	videos.video_category,
	videos.video_link,
	videos.cover_link,
	videos.video_intro,
	topics.topic_name,
	videos.play_num,
	videos.like_num,
	videos.collect_num,
	videos.comment_num

FROM

	videos
	LEFT JOIN users ON videos.user_id = users.id

LEFT JOIN topics ON videos.topic_id=topics.id
WHERE videos.id=1
*/
func (v *Video) FindVideoDetail(db *gorm.DB, id int64) *videorpc.VideoDetailResponse {
	var list *videorpc.VideoDetailResponse
	db.Debug().Model(&Video{}).
		Select(
			"users.nick_name username",
			"videos.id video_id",
			"videos.video_title",
			"videos.video_category",
			"videos.video_link",
			"videos.cover_link",
			"videos.video_intro",
			"topics.topic_name",
			"videos.play_num",
			"videos.like_num",
			"videos.collect_num",
			"videos.comment_num",
		).
		Joins("LEFT JOIN users ON videos.user_id = users.id").
		Joins("LEFT JOIN topics ON videos.topic_id=topics.id").
		Where("videos.id=?", id).
		Find(&list)
	return list
}
func FindVideoList(db *gorm.DB, in *videorpc.VideoListRequest) []*videorpc.VideoList {
	var list []*videorpc.VideoList
	tx := db.Debug().Model(&Video{}).
		Select(
			"users.nick_name username",
			"videos.id video_id",
			"videos.video_title",
			"videos.video_category",
			"videos.video_link",
			"videos.cover_link",
			"videos.video_intro",
			"topics.topic_name",
			"videos.play_num",
			"videos.like_num",
			"videos.collect_num",
			"videos.comment_num",
		).
		Joins("LEFT JOIN users ON videos.user_id = users.id").
		Joins("LEFT JOIN topics ON videos.topic_id=topics.id")
	if in.SortType == 0 {
		tx = tx.Order("play_num desc")
	}
	if in.SortType == 1 {
		tx = tx.Order("videos.created_at desc")
	}
	tx.Scopes(Paginate(int(in.Page), int(in.Size))).Find(&list)
	return list
}
